import requests
from scrapy import Selector
import time


# 文档地址 https://passport.nchu.edu.cn/api/app.asmx
'''
POST /api/app.asmx HTTP/1.1
Host: passport.nchu.edu.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "Passport_APP/JWC_Grkb"
'''

class NCHU():
    def __init__(self, username=None, password=None, token=None):

        self.Username = username

        self.Password = password

        self.Week = '15'

        self.url = 'https://passport.nchu.edu.cn:443/api/app.asmx'

        self.Token = token



    def getToken(self):

        Time = str(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))

        Headers = {
            'User-Agent'     : 'ksoap2-android/2.6.0+',
            'SOAPAction'     : 'Passport_APP/Login',
            'Content-Type'   : 'text/xml;charset=utf-8',
            'Content-Length' : '697',
            'Host'           : 'passport.nchu.edu.cn'
        }

        DATA = '''
        <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
            <v:Header />
            <v:Body>
                <Login xmlns="Passport_APP" id="o0" c:root="1">
                    <SiteID i:type="d:int">23</SiteID>
                    <SignText i:type="d:string">0cb69b0bb6b64114e33e21fb64eb50cc</SignText>
                    <Username i:type="d:string">{}</Username>
                    <Version i:type="d:int">23</Version>
                    <UserDomain i:type="d:string">stu.nchu.edu.cn</UserDomain>
                    <Time i:type="d:string">{}</Time>
                    <Key i:type="d:string">idOocQiuaZ</Key>
                    <Password i:type="d:string">{}</Password>
                </Login>
            </v:Body>
        </v:Envelope>
        '''.format(self.Username, Time, self.Password)

        temp = '<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><Login xmlns="Passport_APP" id="o0" c:root="1"><SiteID i:type="d:int">23</SiteID><SignText i:type="d:string">0cb69b0bb6b64114e33e21fb64eb50cc</SignText><Username i:type="d:string">18085132</Username><Version i:type="d:int">23</Version><UserDomain i:type="d:string">stu.nchu.edu.cn</UserDomain><Time i:type="d:string">{}</Time><Key i:type="d:string">idOocQiuaZ</Key><Password i:type="d:string">hahaha5666LYX</Password></Login></v:Body></v:Envelope>'.format(Time)

        r = requests.post(self.url, data=DATA, headers=Headers)
        r.encoding = r.apparent_encoding
        print('Encoding:' + r.encoding)
        print('Status Code:' + str(r.status_code))
        demo = r.text
        print(r)
        print(demo)
        res = Selector(text=demo)
        self.Token = res.xpath("//jwc_grkbresult/text()").extract_first()
        return self.Token

    def courseInfo(self, week=None):
        if week:
            self.Week = week

        Headers = {
            'User-Agent'     : 'ksoap2-android/2.6.0+',
            'SOAPAction'     : 'Passport_APP/JWC_Grkb',
            'Content-Type'   : 'text/xml;charset=utf-8',
            'Content-Length' : '596',
            'Host'           : 'passport.nchu.edu.cn'
        }

        DATA = '''
        <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
            <v:Header />
                <v:Body>
                    <JWC_Grkb xmlns="Passport_APP" id="o0" c:root="1">
                        <xnxqID i:type="d:string">
                            2020-2021-1
                        </xnxqID>
                        <zc i:type="d:string">{}</zc>
                        <Token i:type="d:string">{}</Token>
                    </JWC_Grkb>
            </v:Body>
        </v:Envelope>
        '''.format(self.Week, self.Token)

        r = requests.post(self.url, data=DATA, headers=Headers)
        r.encoding = r.apparent_encoding
        print('Encoding:' + r.encoding)
        print('Status Code:' + str(r.status_code))
        demo = r.text
        res = Selector(text=demo)
        body = res.xpath("//jwc_grkbresult/text()").extract_first()
        return body